<?php 
$GLOBALS['Pd'] = "N";
require_once($_SERVER['DOCUMENT_ROOT']."/modules/centrodecustos/moduleconf.php");
include($GLOBALS['PAD']."Modules/CentrodeCustos/objops.php");
include($GLOBALS['PAD']."Modules/CentrodeCustos/Lib/op.pettyinvoice.php");
include($GLOBALS['PAD']."Modules/CentrodeCustos/obj.dyn.php"); 
include($GLOBALS['PAD']."Modules/CentrodeCustos/js.jax.php");
include($GLOBALS['PAD']."Modules/CentrodeCustos/func.php");

	if (!isset($_POST['TimeInvoice'])) {
	$_POST['TimeInvoice'] = date("Y-m-d");
	}
	
	$CInvoices = "SELECT DATE(a.TimeInvoice) AS TimeInvoice,
						 DATE(a.TimePay) AS TimePay,
						 z.name AS Status,
						 b.name AS office,
						 b.ref AS off,
						 a.Ref 
						 FROM ccenter_invoices AS a
						 INNER JOIN main_offices AS b ON b.ref = {$_SESSION['office']} 
						 INNER JOIN hcce_status AS z ON z.StatRef = a.Status AND z.Type = 9
						 WHERE a.Type = 9
						 AND  TimeInvoice <= '{$_POST['TimeInvoice']}' 
						 ORDER BY TimeInvoice DESC LIMIT 0,1";
	$Inv = $GLOBALS['CONN']->db_array($CInvoices);
	$Amount = '0,00';
	?>
<script charset="utf-8" type="text/javascript" src="../../CentrodeCustos/Lib/Calcs.js"></script>
<div id="divApp" style="position:absolute; width:600; height:450px; z-index:1000; left: 365px; top: 180px; overflow: 
auto; display:inline; background-color: #F2F2F2; layer-background-color: #F2F2F2; border: 1px none #000000; visibility: hidden;">
</div>
<table width="100%"  border="0" cellpadding="3" cellspacing="1">
  <form action="?action=petty" method="post" name="InsertInvoice" runat="server">
  <input name="CODE" type="hidden" id="CODE" value="0" />
  <input name="Provider" type="hidden" id="Provider" value="0" />
    <tr bgcolor="#99CCCC">
      <td colspan="4" bgcolor="#ACB1CC">
  Petty Cash - De
    <input name="TimeInvoice" type="text" id="TimeInvoice" onFocus="this.select();lcs(this)" 
	  onClick="event.cancelBubble=true;this.select();lcs(this);document.InserInvoice.submit();" 
	  value="<?php echo $Inv[1]['TimeInvoice'];?>" size="8" maxlength="10" />
      a
      <input name="DatePay" type="text" id="DatePay" value="<?php echo $Inv[1]['TimePay'];?>" size="8" maxlength="10" />
      , Estado: <?php echo $Inv[1]['Status'];?>        Loja
          <input name="text" type="text" disabled value="<?php echo $Inv[1]['office'];?>" size="40" /></td>
    </tr>
    <tr bgcolor="#D4D4D4">
      <td colspan="4" valign="top">R&uacute;blicas
      <input name="ArtOffice" type="hidden" id="ArtOffice22" value="<?php echo $Inv[1]['off'];?>" />
      <input name="Invoice" type="hidden" id="ArtTYP" value="<?php echo $Inv[1]['Ref'];?>" /></td>
    </tr>
    <tr bgcolor="#D4D4D4">
      <td colspan="4" valign="top">
	<table width="100%"  border="0" cellpadding="2" cellspacing="1" bgcolor="#EAEAEA">
        <tr bgcolor="#F8F8F8">
          <td colspan="4">Descrição</td>
          <td>Natureza</td>
          <td>Fornecedor</td>
          <td>Funcionário</td>
          <td>Quant.</td>
          <td>PreÇo</td>
          <td>Prom.</td>
          <td>IVA</td>
          <td>Total</td>
          <td rowspan="2" align="center">
		  <input name="AddArticle" value="" type="submit" id="AddArticle2" class="InsertButton"></td>
        </tr>
        <tr bgcolor="#F8F8F8">
          <td colspan="4"><input name="ArtDescription" type="text" id="ArtDescription" size="30" maxlength="255">
		  <div id="OfficeDiv">
		      </div></td>
          <td>-</td>
          <td>-</td>
          <td>
		  <select name="ArtUser" id="ArtUser">
            <option value="0" selected></option>
		  <?php
		$CUsers = "SELECT ref, name FROM main_users WHERE ref_office = {$_SESSION['office']} AND Type = 2 ORDER BY name";
		$Users = $GLOBALS['CONN']->db_array($CUsers);
		for ($u = 1; $u <= $Users[0]; $u++) {
         echo '<option value="'.$Users[$u]['ref'].'">'.$Users[$u]['name'].'</option>';
		 }
		?>
          </select></td>
          <td><input name=ArtQuantity type="text" id=ArtQuantity value="1" size="2" maxlength="10" onKeyUp="ArticleAmount(this.form);"></td>
          <td><input name=ArtUAmount type="text" id=ArtUAmount value="0.00" size="4" maxlength="14" onKeyUp="ArticleAmount(this.form);"></td>
          <td><input name=ArtPromo type="text" id=ArtPromo value="0" size="4" maxlength="6" onKeyUp="ArticleAmount(this.form);"></td>
          <td><select name="ArtTaxPercent" id="select3" onChange="ArticleAmount(this.form);">
              <option value="0" selected>0%</option>
              <option value="5">5%</option>
              <option value="12">12%</option>
              <option value="21">21%</option>
          </select></td>
          <td><input name=ArtAmount type="text" readonly="" id=ArtAmount value="0.00" size="4" maxlength="20"></td>
        </tr>
        <?php
	   $CList = "SELECT 
	      			main_users.name AS User,
					main_offices.name AS Office,
	   				a.ArtReference,
	   				a.Ref,
	   				a.ArtReference,
					a.Description,
					a.Quantity,
					a.UAmount,
					a.IvaPercent,
					a.Promo,
					a.Amount,
					a.CODE,
					a.Provider
	  			FROM ccenter_invarticles AS a
				LEFT OUTER JOIN main_users ON main_users.ref = a.User
				LEFT OUTER JOIN main_offices ON main_offices.ref = a.Office
				WHERE RefInvoice = {$Inv[1]['Ref']} 
				AND a.Office = {$_SESSION['office']}
				AND a.Status != 5";
		$List = $GLOBALS['CONN']->db_array($CList);
	for($r = 1; $r <= $List[0]; $r++) {
  echo '<tr bgcolor="#B0C0D0">
        <td colspan="4">'.$List[$r]['Description'].'</td>

        <td>'.$List[$r]['CODE'].'</td>
        <td>'.$List[$r]['Provider'].'</td>
        <td>'.$List[$r]['User'].'</td>
        <td align="right">'.$List[$r]['Quantity'].'</td>
        <td align="right">'.$List[$r]['UAmount'].'</td>
        <td>'.$List[$r]['Promo'].'</td>
        <td>'.round($List[$r]['IvaPercent']).'%</td>
        <td align="right">'.$List[$r]['Amount'].'</td>
		<td><input class="delbutton" name="RemArticle" alt="Eliminar" value="'.$List[$r]['Ref'].'" type="submit"></td>
		</tr>';
	  $Amount = $Amount+$List[$r]['Amount'];?>
        <?php } ?>
    </table>
	</td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
	  <td colspan="3">Transporte: <?php 

	   $CList = "SELECT 
					SUM(a.Amount) AS Amount
	  			FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON b.Ref = a.RefInvoice
				WHERE a.Office = {$_SESSION['office']}
				AND b.TYPE = 9
				AND b.Status = 2
				AND b.TimeClose <= '{$Inv[1]['TimeInvoice']}'
				GROUP BY b.Ref
				ORDER BY b.TimeClose DESC LIMIT 0,1";
		$List = $GLOBALS['CONN']->db_array($CList);
		
	$QPlaf = "SELECT 
					SUM(ccenter_petty.Plafond) AS Plaf
	  			FROM ccenter_petty
				WHERE Office = {$_SESSION['office']}
				AND sysTime > {$Inv[1]['TimeInvoice']} AND ('{$Inv[1]['TimePay']}' OR '{$Inv[1]['TimePay']}' = '')
				ORDER BY sysTime DESC
				LIMIT 0,1";
		$Plafond = $GLOBALS['CONN']->db_array($QPlaf);
		
		if (!isset($Plafond[1])) {
		$Plaf = 0;
		} else {
		$Plaf = $Plafond[1]['Plaf'];
		}
		
		if (!isset($List[1])) {
		$Tranp = 0;
		} else {
		$Tranp = $List[1]['Amount'];
		} ?>
	  
	  EUR <?php echo $Plaf-$Tranp;?></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
	  <td colspan="3"><b>Total: EUR </b>
        <input name="Amount" type="text" id="Amount" readonly="" value="<?php echo $Amount;?>" size="14" maxlength="20"></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
    <td colspan="3">Saldo: EUR <?php echo $Plaf-$Amount;?>   
	</td>
    </tr>
  </form>
</table>